﻿Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraGrid.Views.Grid

Public Class TheKho1
    Private Sub LoadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        Me.DMHangHoaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMHangHoa)
        Me.TuNgayIDateEdit.EditValue = getNgayDauThang(Today)
        Me.DenNgayIDateEdit.EditValue = getNgayCuoiThang(Today)
        Me.IGridControl.ForceInitialize()
        Me.IGridView1.ExpandAllGroups()
        Me.setHeaderFooter(Main.TenDoanhNghiep.ToString, Main.DiaChiDoanhNghiep.ToString)
        Me.LoaihhISearchLookupEdit.EditValue = Me.PCV_CLINICDataSet.DMHangHoa.Rows(0).Item("ma").ToString
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub TheKho1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        LoadDuLieu()
        setSoLuongCuoiKy()
    End Sub
    Private Sub setSoLuongCuoiKy() 'cập nhật lấy đầu kỳ của nhiều lô
        Dim tondauky, soluongnhap, soluongxuat As Decimal
        Dim solo As String = Nothing
        Dim handung As String = Nothing
        Me.IGridView1.ClearGrouping()
        Try
            For index = 0 To Me.IGridView1.RowCount - 1
                If index = 0 Then
                    If True Then
                        tondauky = CDec(Me.IGridView1.GetRowCellValue((index), "soluongdauky"))
                        solo = Me.IGridView1.GetRowCellValue((index), "solo").ToString
                        handung = CDec(Me.IGridView1.GetRowCellValue((index), "soluongdauky"))
                    Else
                        Me.PCV_CLINICDataSetTongHop.getTCTonKho_TheKho_TheoLo_HanDung.Rows(index)("soluongcuoiky") = CDec(Me.IGridView1.GetRowCellValue((index), "soluongdauky")) + CDec(Me.IGridView1.GetRowCellValue((index), "soluongnhap")) - CDec(Me.IGridView1.GetRowCellValue((index), "soluongxuat"))
                    End If
                Else
                    tondauky = CDec(Me.IGridView1.GetRowCellValue((index - 1), "soluongcuoiky"))
                    Dim solonext As String = Me.IGridView1.GetRowCellValue((index), "solo").ToString
                    If solonext <> solo Then 'không phải số lô cũ - qua lô mới
                        soluongnhap = CDec(Me.IGridView1.GetRowCellValue((index), "soluongnhap"))
                        soluongxuat = CDec(Me.IGridView1.GetRowCellValue((index), "soluongxuat"))
                        tondauky = CDec(Me.IGridView1.GetRowCellValue((index), "soluongdauky"))
                        Me.PCV_CLINICDataSetTongHop.getTCTonKho_TheKho_TheoLo_HanDung.Rows(index)("soluongcuoiky") = tondauky + soluongnhap - soluongxuat
                        solo = solonext
                    End If
                End If
                soluongnhap = CDec(Me.IGridView1.GetRowCellValue((index), "soluongnhap"))
                soluongxuat = CDec(Me.IGridView1.GetRowCellValue((index), "soluongxuat"))
                Me.PCV_CLINICDataSetTongHop.getTCTonKho_TheKho_TheoLo_HanDung.Rows(index)("soluongcuoiky") = tondauky + soluongnhap - soluongxuat
            Next
            setGrid()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub setGrid()
        If Me.RadioGroup1.SelectedIndex = 0 Then
            Me.colsolo.Visible = True
            Me.colhsd.Visible = True
            Me.colsolo.GroupIndex = 1
            Me.colhsd.GroupIndex = 2
            Me.IGridView1.ExpandAllGroups()
            Me.IGridView1.setTitle("Thẻ kho lô - hạn dùng : " & Me.LoaihhISearchLookupEdit.Text)
        Else
            Me.colsolo.Visible = False
            Me.colhsd.Visible = False
            Me.colsolo.GroupIndex = -1
            Me.colhsd.GroupIndex = -1
            Me.IGridView1.setTitle("Thẻ kho tổng hợp : " & Me.LoaihhISearchLookupEdit.Text)
        End If
    End Sub
    Private Sub DrillDown()
        Try
            Dim ma As String = Me.IGridView1.GetFocusedRowCellValue("ma")
            If ma.Substring(0, 4) = "XHDP" Then
                XuatHang.setMa(ma)
                XuatHang.Show()
                XuatHang.MdiParent = Me.MdiParent
                XuatHang.WindowState = FormWindowState.Maximized
            ElseIf ma.Substring(0, 4) = "NHDP" Then
                NhapHang.setMa(ma)
                NhapHang.Show()
                NhapHang.MdiParent = Me.MdiParent
                NhapHang.WindowState = FormWindowState.Maximized
            End If
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show("Không có dữ liệu gốc !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub RadioGroup1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadioGroup1.SelectedIndexChanged
        setTheKho()
    End Sub
    Private Sub TruyCậpDữLiệuGốcToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TruyCậpDữLiệuGốcToolStripMenuItem.Click
        DrillDown()
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        Me.BarButtonItemLamTuoi.PerformClick()
    End Sub
    Private Sub setTheKho()
        If Me.RadioGroup1.SelectedIndex = 0 Then
            Me.GetTCTonKho_TheKho_TheoLo_HanDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTCTonKho_TheKho_TheoLo_HanDung, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), Me.LoaihhISearchLookupEdit.EditValue, "TL")
        Else
            Me.GetTCTonKho_TheKho_TheoLo_HanDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTCTonKho_TheKho_TheoLo_HanDung, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), Me.LoaihhISearchLookupEdit.EditValue, "")
        End If
        setSoLuongCuoiKy()
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        setTheKho()
    End Sub
    Private Sub BarButtonItemBaoCao_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Me.IGridView1.inNhanh()
    End Sub
    Private Sub LoaihhISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles LoaihhISearchLookupEdit.EditValueChanged
        setTheKho()
    End Sub
End Class
